package BackgammonBot.JGammon;

import BackgammonBot.Algorithmus.AlphaBetaAlgo;
import BackgammonBot.Algorithmus.AlphaBetaAlgoMultiThread;
import BackgammonBot.Algorithmus.AlphaBetaAlgoThreadStart;
import BackgammonBot.GameIO.IOGameFiles;
import BackgammonBot.GameLogic.BackgammonLogic;
import jgam.Move;
import jgam.Player;
import jgam.SingleMove;

public class AIPlayer extends Player
{
	public AIPlayer(String name) {
        super(name);
    }
	
	@Override
	public void handle(Object msg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Move move() throws Exception 
	{
		int[] blackBoard = this.getGame().getSnapshot().getBlueBoard();
		int[] whiteBoard = this.getGame().getSnapshot().getWhiteBoard();
		
		byte[] board = new byte[26];
		for (int i = 1; i < 25; i++) 
		{
			if (whiteBoard[i] > 0)
				board[25 - i] = (byte) -whiteBoard[i];
			if (blackBoard[i] > 0)
				board[i] = (byte) blackBoard[i];
		}
		
		BackgammonLogic gameLogic = new BackgammonLogic(board
				, (byte) whiteBoard[0]
				, (byte) whiteBoard[25]
				, (byte) blackBoard[0]
				, (byte) blackBoard[25]
				, this.getGame().getSnapshot().whitesTurn ? 'w' : 'b'
				, this.getGame().getDice());
		
		AlphaBetaAlgo alhpaBeta = new AlphaBetaAlgo(2, gameLogic);
		//AlphaBetaAlgoThreadStart alhpaBeta = new AlphaBetaAlgoThreadStart();
//		alhpaBeta.run(3, board
//				, (byte) whiteBoard[0]
//				, (byte) whiteBoard[25]
//				, (byte) blackBoard[0]
//				, (byte) blackBoard[25]
//				, this.getGame().getSnapshot().whitesTurn ? 'w' : 'b'
//				, this.getGame().getDice());
		
		SingleMove[] moves = new SingleMove[alhpaBeta.getMove().size()];
		for (int i = 0; i < alhpaBeta.getMove().size(); i++) 
		{
			SingleMove move;
			
			//if ((this.getGame().getSnapshot().whitesTurn ? 'w' : 'b') == 'w')
			if (gameLogic.getInitialPlayer() == 'w')
			{
				boolean beat = this.getOtherPlayer().getBoard()[alhpaBeta.getMove().get(i)[1]] > 0;
				
				move = new SingleMove(this, 25 - alhpaBeta.getMove().get(i)[0], 25 - alhpaBeta.getMove().get(i)[1], beat);
			}
			else
			{
				boolean beat = this.getOtherPlayer().getBoard()[25 - alhpaBeta.getMove().get(i)[1]] > 0;
				
				move = new SingleMove(this, alhpaBeta.getMove().get(i)[0], alhpaBeta.getMove().get(i)[1], beat);
			}
			moves[i] = move;
		}
		
		return new AIMove(moves, this);
		
	}

	@Override
	public int nextStep(boolean rollOnly) throws Exception {
		return ROLL;
	}

	@Override
	public boolean acceptsOffer(int offer) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isRemote() {
		return false;
	}

	@Override
	public void informRoll() throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void informMove(SingleMove move) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void informAccept(boolean answer) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isWaitingForUIMove() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void animateMove(Move m) {
		// TODO Auto-generated method stub
		
	}
	
}
